//
//  YMNetworkManager.swift
//  Driver
//
//  Created by lantaiyuan on 2017/9/22.
//  Copyright © 2017年 youmy. All rights reserved.
//

import UIKit
import AFNetworking

enum YMHTTPMethod {
    case GET
    case POST
}

class YMNetworkManager: AFHTTPSessionManager {
    
    static let shared:YMNetworkManager = {
        let instance = YMNetworkManager()
        
        instance.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return instance
    }()
    
    
    func request(method: YMHTTPMethod,
                 URLString: String,
                 params: [String: AnyObject]?,
                 completion:@escaping(_ json: Any?, _ isSuccess: Bool)->()) {
        // 成功回调
        let success = {(task: URLSessionDataTask, json: Any?) ->() in
            completion(json, true)
        }
        
        // 失败回调
        let failure = {(task: URLSessionDataTask?, error: Error?)->() in
            print("Error: " + error.debugDescription )
            completion(nil, false)
        }
        
        if method == .GET {
            get(URLString, parameters: params, progress: nil, success: success, failure: failure)
        }else{
            post(URLString, parameters: params, progress: nil, success: success, failure: failure)
        }
    }
}
